package com.vf.admin.client.dao;

import com.vf.admin.common.db.BaseDao;
import com.vf.admin.common.splitpage.SplitPageBean;
import com.vf.admin.miaodong.entity.AppIntimacyGuard;
import com.vf.admin.miaodong.entity.AppMingXiShouLi;
import com.vf.admin.miaodong.entity.AppMingxiMeilizhi;
import com.vf.admin.utils.StringEx;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 账单明细-充值
 */
@Repository
public class AppMingxiMeilizhiDao extends BaseDao<AppMingxiMeilizhi> {

    /**
     * 查询列表
     */
    public List<AppMingxiMeilizhi> findList(Integer userid, SplitPageBean splitPage) {
        ArrayList<Object> paramList = new ArrayList<Object>();
        StringBuilder sbStr = new StringBuilder("select * from app_mingxi_meilizhi where 1=1  ");
        sbStr.append(" and userid=? ");
        paramList.add(userid);
        sbStr.append(" order by createtime desc");
        String sql = sbStr.toString();
        return this.queryList(sql,paramList, AppMingxiMeilizhi.class,splitPage);
    }


    /**
     * 详情
     */
    public AppMingxiMeilizhi get(Integer id) {
        if (StringEx.isNullOrLing(id)) {
            return null;
        }
        List<Object> paramList = new ArrayList<Object>();
        StringBuilder sbStr = new StringBuilder("select * from app_mingxi_meilizhi where 1=1");
        sbStr.append(" and id = ? ");
        paramList.add(id);
        String sql = sbStr.toString();
        return this.queryObj(sql, paramList, AppMingxiMeilizhi.class);
    }

    /**
     * 添加/修改
     */
    public void save(AppMingxiMeilizhi haoYou) {
        if (haoYou.getId() != null && haoYou.getId() > 0) {
            this.update(haoYou);
        } else {
            haoYou.setId(this.add(haoYou));
        }
    }

    /**
     * 更新用户头像
     * @param userid
     * @param pic 头像地址
     */
    public void updateImg(Integer userid,String pic){
        if(userid==null || userid<=0 || StringEx.isNull(pic)){
            return ;
        }
        String sql = " UPDATE app_mingxi_meilizhi SET pic = ?  WHERE userid = ? ";
        ArrayList<Object> paramList = new ArrayList<Object>();
        paramList.add(pic);
        paramList.add(userid);
        this.update(sql,paramList);
    }
    /**
     * 更新用户昵称
     * @param userid
     * @param nick 昵称
     */
    public void updateNick(Integer userid,String nick){
        if(userid==null || userid<=0 || StringEx.isNull(nick)){
            return ;
        }
        String sql = " UPDATE app_mingxi_meilizhi SET nick = ?  WHERE userid = ? ";
        ArrayList<Object> paramList = new ArrayList<Object>();
        paramList.add(nick);
        paramList.add(userid);
        this.update(sql,paramList);
    }

    public List<AppMingxiMeilizhi> getDayRanking(Map<String, Object> params)
    {
        ArrayList<Object> list = new ArrayList<>();
        StringBuilder sb = new StringBuilder("select *,sum(num) as totalMeiLi from app_mingxi_meilizhi where to_days(createtime) = to_days(now())");
        if(params.containsKey("hiddenUserIds") && StringEx.isNotNull(params.get("hiddenUserIds"))) {
            sb.append(" AND userid not in ("+ params.get("hiddenUserIds") +")");
        }
        sb.append(" GROUP BY userId ORDER BY totalMeiLi desc limit 100");
        return this.queryList(sb.toString(),list, AppMingxiMeilizhi.class);
    }


    /**
     * 周排行
     * @return
     */
    public List<AppMingxiMeilizhi> getWeekRanking(Map<String, Object> params)
    {
        ArrayList<Object> list = new ArrayList<>();
        StringBuilder sb = new StringBuilder("select *,sum(num) as totalMeiLi from app_mingxi_meilizhi where YEARWEEK( date_format(  createtime,'%Y-%m-%d' ) ) = YEARWEEK( now() )");
        if(params.containsKey("hiddenUserIds") && StringEx.isNotNull(params.get("hiddenUserIds"))) {
            sb.append(" AND userid not in ("+ params.get("hiddenUserIds") +")");
        }
        sb.append(" GROUP BY userId ORDER BY totalMeiLi desc limit 100");
        return this.queryList(sb.toString(),list,AppMingxiMeilizhi.class);
    }

    /**
     * 月排行
     * @return
     */
    public List<AppMingxiMeilizhi> getMonthRanking(Map<String, Object> params)
    {
        ArrayList<Object> list = new ArrayList<>();
        StringBuilder sb = new StringBuilder("select *,sum(num) as totalMeiLi from app_mingxi_meilizhi where DATE_FORMAT( createtime, '%Y%m' ) = DATE_FORMAT( CURDATE() , '%Y%m' )");
        if(params.containsKey("hiddenUserIds") && StringEx.isNotNull(params.get("hiddenUserIds"))) {
            sb.append(" AND userid not in ("+ params.get("hiddenUserIds") +")");
        }
        sb.append(" GROUP BY userId ORDER BY totalMeiLi desc limit 100");
        return this.queryList(sb.toString(),list,AppMingxiMeilizhi.class);
    }


}
